package com.objecteffects.template.web.utils;

import java.util.List;

import com.google.inject.Inject;

import com.objecteffects.template.model.TumblrBlogEntity;
import com.objecteffects.template.model.UserEntity;
import com.objecteffects.template.service.UserEntityService;

import org.scribe.model.Token;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Rusty Wright
 */
public class TumblrAccessToken {
    private final transient Logger log = LoggerFactory
        .getLogger(TumblrAccessToken.class);

    private final UserEntityService userEntityService;

    @Inject
    TumblrAccessToken(final UserEntityService _userEntityService) {
        this.userEntityService = _userEntityService;
    }

    public void saveToken(final String tumblrUrl, final Token accessToken,
        final Long userId) {
        final UserEntity userEntity = this.userEntityService.findById(userId);

        final TumblrBlogEntity blogEntity = this.findEntity(tumblrUrl, userId);

        if (blogEntity != null) {
            this.log.debug("setting access token on: {}", tumblrUrl);

            blogEntity.setAccessToken(accessToken);
        }
        else {
            final TumblrBlogEntity tumblrBlogEntity =
                new TumblrBlogEntity(tumblrUrl, accessToken);

            this.log.debug("adding blog entity: {}", tumblrUrl);

            userEntity.getTumblrBlogEntities().add(tumblrBlogEntity);
        }

        this.userEntityService.update(userEntity);
    }

    public Token fetchToken(final String tumblrUrl, final Long userId) {
        final TumblrBlogEntity blogEntity = this.findEntity(tumblrUrl, userId);

        if (blogEntity == null) {
            return null;
        }

        this.log.debug("found blog entity: {}", tumblrUrl);

        return blogEntity.getAccessToken();
    }

    public TumblrBlogEntity
    findEntity(final String tumblrUrl, final Long userId) {
        final UserEntity userEntity = this.userEntityService.findById(userId);

        final List<TumblrBlogEntity> tumblrBlogEntities =
            userEntity.getTumblrBlogEntities();

        this.log.debug("looking for: {}", tumblrUrl);

        for (final TumblrBlogEntity eachBlogEntity : tumblrBlogEntities) {
            this.log.debug("checking blog entity: {}", eachBlogEntity);

            if (eachBlogEntity.getUrls() == null) {
                this.log.warn("userEntity tumblrBlogEntities urls is null: {}",
                              eachBlogEntity);

                continue;
            }

            for (final String url : eachBlogEntity.getUrls()) {
                this.log.debug("checking url: {}", url);

                if (tumblrUrl.equalsIgnoreCase(url)) {
                    this.log.debug("found blog entity: {}", eachBlogEntity);

                    return eachBlogEntity;
                }
            }
        }

        this.log.debug("blog entity not found for: {}", tumblrUrl);

        return null;
    }

    public void saveTokenOld(final String tumblrUrl, final Token accessToken,
        final Long userId) {
        final UserEntity userEntity = this.userEntityService.findById(userId);

        final List<TumblrBlogEntity> tumblrBlogEntities =
            userEntity.getTumblrBlogEntities();

        this.log.debug("looking for: {}", tumblrUrl);

        for (final TumblrBlogEntity eachBlogEntity : tumblrBlogEntities) {
            this.log.debug("checking tumblrBlogEntity: {}", eachBlogEntity);

            if (eachBlogEntity.getUrls() == null) {
                this.log.warn("userEntity tumblrBlogEntities urls is null: {}",
                              eachBlogEntity);

                continue;
            }

            for (final String url : eachBlogEntity.getUrls()) {
                this.log.debug("checking url: {}", url);

                if (tumblrUrl.equalsIgnoreCase(url)) {
                    this.log.debug("setting access token for: {}",
                                   eachBlogEntity);

                    eachBlogEntity.setAccessToken(accessToken);

                    this.userEntityService.update(userEntity);

                    return;
                }
            }
        }

        final TumblrBlogEntity tumblrBlogEntity =
            new TumblrBlogEntity(tumblrUrl, accessToken);

        userEntity.getTumblrBlogEntities().add(tumblrBlogEntity);

        this.userEntityService.update(userEntity);
    }

    public Token fetchTokenOld(final String tumblrUrl, final Long userId) {
        final UserEntity userEntity = this.userEntityService.findById(userId);

        final List<TumblrBlogEntity> tumblrBlogEntities =
            userEntity.getTumblrBlogEntities();

        this.log.debug("looking for: {}", tumblrUrl);

        for (final TumblrBlogEntity eachBlogEntity : tumblrBlogEntities) {
            this.log.debug("checking tumblrBlogEntity: {}", eachBlogEntity);

            if (eachBlogEntity.getUrls() == null) {
                this.log.warn("userEntity tumblrBlogEntities urls is null: {}",
                              eachBlogEntity);

                continue;
            }

            for (final String url : eachBlogEntity.getUrls()) {
                this.log.debug("checking url: {}", url);

                if (tumblrUrl.equalsIgnoreCase(url)) {
                    this.log.debug("getting access token for: {}",
                                   eachBlogEntity);

                    return eachBlogEntity.getAccessToken();
                }
            }
        }

        return null;
    }
}
